﻿using System;
using System.Collections.Generic;
using System.Text;
using TvControl;
using System.Diagnostics;
using System.IO;
using TvEngine;

namespace TvServerPlugin
{
    public class MPWebServices : ITvServerPlugin
    {
        private Cassini.Server _webServer = null;

        /// <summary>
        /// returns the name of the plugin
        /// </summary>
        public string Name
        {
            get { return "MediaPortal WebServices"; }
        }
        /// <summary>
        /// returns the version of the plugin
        /// </summary>
        public string Version
        {
            get { return "1.1.0.0"; }
        }
        /// <summary>
        /// returns the author of the plugin
        /// </summary>
        public string Author
        {
            get { return "Chris Brock"; }
        }
        /// <summary>
        /// returns if the plugin should only run on the master server
        /// or also on slave servers
        /// </summary>
        public bool MasterOnly
        {
            get { return true; }
        }

        public void Start(IController controller)
        {
            System.Threading.Thread th = new System.Threading.Thread(StartPlugin);
            th.Start();
        }
        public void Stop()
        {
            if (_webServer != null)
            {
                Log.WriteInfo("Plugin stopped");
                _webServer.Stop();
                _webServer = null;
            }
        }

        public SetupTv.SectionSettings Setup
        {
            get { return new MPWebServicesSetup(); }
        }
        
        private void CheckUpdateDll(string dll)
        {
            string source = Settings.BaseDir.Substring(0, Settings.BaseDir.Length - 7) + dll;
            string target = Settings.HTDocsDirectory + "\\bin\\" + dll;
            
            string sourceVersion = FileVersionInfo.GetVersionInfo(source).FileVersion;
            string targetVersion = "";
            if (File.Exists(target))
                targetVersion = FileVersionInfo.GetVersionInfo(target).FileVersion;

            if (sourceVersion != targetVersion)
            {
                try
                {
                    File.Copy(source, target, true);
                }
                catch (Exception ex)
                {

                }
                Log.WriteInfo("Updated " + dll);
            }
            else
                Log.WriteInfo("No update to " + dll + " needed.");
        }

        private void StartPlugin()
        {
            Log.WriteInfo("Starting plugin");
            if (!Settings.LoadSettings())
            {
                Log.WriteError("Settings are invalid. Can't start web server!!!");
                return;
            }

            CheckUpdateDll("DirectShowLib.dll");
            CheckUpdateDll("TvControl.dll");
            CheckUpdateDll("TvDatabase.dll");
            CheckUpdateDll("TvLibrary.Interfaces.dll");

            Log.WriteInfo("Starting web server...");
            try
            {
                _webServer = new Cassini.Server(Settings.HttpPort, Settings.VirtualDirectory, Settings.HTDocsDirectory);
                _webServer.Start();
            }
            catch (Exception ex)
            {
                Log.WriteError("Exception raised while trying to start webserver. " + ex.Message);
                _webServer = null;
                return;
            }
            Log.WriteInfo("Web-Server started.");
        }
    }
}
